//
//  LKSportPolyline.m
//  SportDemo
//
//  Created by Lukj on 2017/4/23.
//  Copyright © 2017年 lukj. All rights reserved.
//

#import "LKSportPolyline.h"

@implementation LKSportPolyline
//  oc2.0之前, @synthesize负责实现属性的get&set方法并且生成成员变量, 不是懒加载机制, @property只负责声明属性的get&set方法, oc2.0之后,@property负责声明属性的get&set方法又可以实现属性的get&set方法
@synthesize time = _time;
@synthesize distance = _distance;
+ (instancetype)polylineWithSourceLocation:(CLLocation *)source andWithDestLocation:(CLLocation *)dest {
    
    //构造折线数据对象
    CLLocationCoordinate2D commonPolylineCoords[2];
    //  定位点
    commonPolylineCoords[0].latitude = source.coordinate.latitude;
    commonPolylineCoords[0].longitude = source.coordinate.longitude;
    //  点击点
    commonPolylineCoords[1].latitude = dest.coordinate.latitude;
    commonPolylineCoords[1].longitude = dest.coordinate.longitude;
    
    //构造折线对象
    LKSportPolyline *commonPolyline = [LKSportPolyline polylineWithCoordinates:commonPolylineCoords count:2];
    //  计算平均速度
    [commonPolyline calculateSpeedWithSource:source andDest:dest];
    
    return commonPolyline;
}

//  计算速度 速度的颜色
- (void)calculateSpeedWithSource:(CLLocation *)source andDest:(CLLocation *)dest {
    //  (起点速度+终点速度) * 0.5 m/s -> km/h /1000 * 60 * 60
    _speed = (source.speed + dest.speed) * .5 * 3.6;
    
    //  通过速度改变折线的颜色 red 0~1 speed 0~30
    _polylineColor = [UIColor colorWithRed:(_speed * 0.033) green:1 blue:0 alpha:1];
    
    //  计算每段折线时长
    _time = [dest.timestamp timeIntervalSinceDate:source.timestamp];
    //  计算每段折线的距离
    _distance = [dest distanceFromLocation:source] * 0.001;
}


#pragma mark - get

- (CGFloat)distance {
    
    return _distance;
}

- (CGFloat)time {
    
    return _time;
}

@end
